﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class DialogImport09SalesmenID : Form
    {
        public DialogImport09SalesmenID(int addressAreaID)
        {
            InitializeComponent();

            try
            {
                this.dataGridViewSalesmenID.AutoGenerateColumns = false;

                CommonMetaList commonMetaList = new CommonMetaList();

                ERmgrUIP.ListAddressAreaDataTable listItemAddressAreaDataTable = commonMetaList.GetAddressArea();
                this.comboBoxAddressAreaID.DataSource = listItemAddressAreaDataTable;
                this.comboBoxAddressAreaID.DisplayMember = listItemAddressAreaDataTable.DescriptionColumn.ColumnName;
                this.comboBoxAddressAreaID.ValueMember = listItemAddressAreaDataTable.AddressAreaIDColumn.ColumnName;
                this.comboBoxAddressAreaID.DataBindings.Add("SelectedValue", this, "AddressAreaID", true, DataSourceUpdateMode.OnPropertyChanged);

                this.AddressAreaID = addressAreaID;
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }

        private int addressAreaID;

        public int AddressAreaID
        {
            get { return this.addressAreaID; }
            set
            {
                if (this.addressAreaID != value)
                {
                    this.addressAreaID = value;

                    if (this.addressAreaID > 0)
                        this.dataGridViewSalesmenID.DataSource = SQLDatabase.GetDataTable("SELECT SalesmenID, DescriptionOfficial, DescriptionLastName FROM ListSalesmenName WHERE InActive = 0 AND (" + this.AddressAreaID + " = -1 OR SalesmenID IN (SELECT SalesmenID FROM ListSalesmenAddressArea WHERE AddressAreaID = " + this.AddressAreaID + ")) ORDER BY DescriptionOfficial, DescriptionLastName");
                    else
                        this.dataGridViewSalesmenID.DataSource = null;
                }
            }
        }

        private int salesmenID;

        public int SalesmenID
        {
            get { return this.salesmenID; }
            private set { this.salesmenID = value; }
        }

        private void toolStripButtonEscapeAndNext_Click(object sender, EventArgs e)
        {
            try
            {
                if (sender.Equals(this.toolStripButtonNext))
                {
                    if (this.dataGridViewSalesmenID.CurrentRow == null) { this.DialogResult = DialogResult.None; return; }

                    DataRow dataRowSalesmenID = ((DataRowView)this.dataGridViewSalesmenID.CurrentRow.DataBoundItem).Row;
                    if (dataRowSalesmenID != null && dataRowSalesmenID["SalesmenID"] != DBNull.Value)
                    {
                        this.SalesmenID = (int)dataRowSalesmenID["SalesmenID"];
                        this.DialogResult = DialogResult.OK;
                    }
                }

                
                if (sender.Equals(this.toolStripButtonEscape))
                    this.DialogResult = DialogResult.Cancel;

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
                this.DialogResult = DialogResult.None;
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridViewSalesmenID_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dataGridView = (DataGridView)sender;
            using (SolidBrush b = new SolidBrush(dataGridView.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 4);
            }
        }

    }
}
